<template>
  <div class="login-container">
    <el-form class="login-form" :model="loginForm" :rules="loginRules" ref="loginFromRef">
      <div class="title-container">
        <!-- 使用国际化语言包 通过i18n文件语言包 -->
        <h3 class="title">{{$t('msg.login.title')}}</h3>
        <lang-select class="lang-select"></lang-select>
      </div>

      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="username" type="text" />
      </el-form-item>

      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" show-password placeholder="password" />
      </el-form-item>
      <!-- 登录按钮 -->
      <el-button type="primary" style="width: 100%; margin-bottom: 30px" :loading="loading" @click="handLogin">
        {{$t('msg.login.loginBtn')}}
      </el-button>

      <!-- 底部提示区域 -->
      <div class="tips" v-html="$t('msg.login.desc')"></div>

    </el-form>
  </div>
</template>


<script setup>
//导入切换国际化的组件
import LangSelect from '@/components/LangSelect/index'
import { ref ,computed} from 'vue'
import {useI18n} from 'vue-i18n'
//导入rules文件中的表单验证函数
import { validatePassword } from './rules'
//导入vuex
import { useStore } from 'vuex'

// 数据源
const loginForm = ref({
  username: 'super-admin',
  password: '123456'
})
// 验证规则
const i18n = useI18n()
const loginRules = ref({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: i18n.t('msg.login.usernameRule'),
      message:computed(()=>{
        return i18n.t('msg.login.usernameRule')
      })
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: validatePassword()
    }
  ]
})

//登录动作处理
const loading = ref(false)
//设置会null他会自动找refg为loginFromRef的元素
const loginFromRef = ref(null)
const store = useStore()
//登录按钮触发时间
const handLogin = () => {
  console.log(i18n.t('msg.login.usernameRule'));
  loginFromRef.value.validate(valid => {
    //验证不通过不往下执行
    if (!valid) return
    //把按钮设置为加载中状态
    loading.value = true
    //调用vuex actiond的login方法
    store.dispatch('user/login', loginForm.value).then(() => {
      loading.value = false
      //TODO: 登录后操作
    }).catch(err => {
      console.log(err);
      loading.value = false
    })
  })
}


</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    ::v-deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;

      .el-form-item__content {
        .el-input__wrapper {
          background-color: #283443 !important;
          box-shadow: 0 0 0 1px #283443 inset !important;
          width: 114%;
        }
      }

    }

    ::v-deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 45px;
        caret-color: $cursor;
      }
    }

    //底部提示区域
    .tips{
      font-size: 16px;
      color: #fff;
      line-height: 30px;
    }
  }


  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  //国际化样式
  .lang-select{
    position: absolute;
    top: 4px;
    right: 0;
    background-color: #fff;
    font-size: 22px;
    padding: 4px;
    border-radius: 4px;
    cursor: pointer;
  }
}
</style>

